Learning Flex Basics (Part 4): Working with Data Models
Table of Contents
- Introduction
- Create the Application Object and Application Layout
- Add the Form Controls and the DataGrid Controls
- Add the New Employee Model, Employee, and Location Models
- Instantiate the DataController Class, and Create the Employee and DataController Classes
- Add the DataController Methods
- Code Listing for data.mxml
- Code Listings for DataController.as and Employee.as
- Code Listings for locations.xml and employee.xml
- Where to Go From Here
Instantiate the DataController Class
To complete the data.mxml file, you instantiate the DataController class (which you build later). This is a straightforward procedure that is discussed in more detail in Learning Flex Basics (Part 2): Creating a Flex Calculator. You can instantiate an ActionScript class using a tag in an MXML file, but you must include a namespace reference for the location of the class so that the Flex compiler can locate it.
-
Add an additional
xmlnsproperty to theApplicationtag; give the value of the namespace the value of "*" and do not assign a prefix to it:<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" xmlns="*">
-
After the Model tags, add a DataController tag with an
id="dataController":<DataController id="dataController"/>
Note: You have already referenced this object in specifying handlers for the Button and DataGrids.
- Save the file. The data.mxml file is now complete.
Create the Employee Class
You now create the first of two external ActionScript classes required for the application. The Employee.as class is not used directly by data.mxml; instead it is imported into the DataController class you build next and used there as factory to generate new employee objects.
The class is simple, consisting of an empty constructor, properties for the employee name and location, and a method that assigns passed values to these properties.
The general structure of ActionScript classes is covered in Learning Flex Basics (Part 2): Creating a Flex Calculator.
- Open a new file in your editor and save it as Employee.as in the flex_tutorials directory.
-
Add a class definition and empty constructor for the Employee class:
class Employee { // constructor public function Employee() {} } -
Above the constructor, add three undefined properties of type String for
fName,lName, andlocation:public var fName:String; public var lName:String; public var location:String;
-
After the constructor, add a public method called
addEmpDatathat takes the parametersfName,lName, andlocation(all type String), and sets the properties of the same name equal to the passed parameters:public function addEmpData(fName:String,lName:String,location:String):Void { this.fName=fName; this.lName=lName; this.location=location; } - Save the file.
Create the DataController Class
Next you create the DataController class, for which you created the instantiation in data.mxml earlier. This class contains methods used to handle events in data.mxml. This class will import two addtional classes:
- Employee: The ActionScript class you just created
- mx.managers.DragManager: A static class from the Flex class library that contains properties and methods used in handling drag-and-drop event
You will use only a single property of the DragManager class here, but it has much more functionality that can help you implement business rules around drag and drop operations. Consult the Flex documentation for more details.
- Open a new file in your editor and save it as DataController.as in the flex_tutorials directory.
-
At the top of the file, add import statements to import the Employee and mx.managers.DragManager classes:
import mx.managers.DragManager; import Employee;
-
After the import statements, add a class definition with an empty constructor for the DataController class:
class DataController { // constructor public function DataController() {} }